package com.qzxy.premiumhotelsystem.handler;


import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.util.SaResult;
import com.qzxy.premiumhotelsystem.common.annotation.RateLimiter;
import com.qzxy.premiumhotelsystem.common.exception.RateLimitException;
import com.qzxy.premiumhotelsystem.common.exception.SqlException;
import com.qzxy.premiumhotelsystem.common.result.Result;
import com.qzxy.premiumhotelsystem.common.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static com.qzxy.premiumhotelsystem.common.result.ResultCodeEnum.INVALID_REQUEST;


/**
 * 全局异常处理器，处理项目中抛出的业务异常
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    /**
     * 处理全部异常
     */
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception ex){
        log.error("异常信息：{}",ex.getMessage());
        ex.printStackTrace();
        return Result.build(ex.getMessage(), ResultCodeEnum.SERVER_ERROR);

    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public Result handleBindException(BindException e) {
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return Result.build(message, INVALID_REQUEST);
    }

    @ExceptionHandler(RateLimitException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result handleCustomException(RateLimitException ex) {
        ex.printStackTrace();
        return Result.error(ex.getMessage());
    }


    /**
     * sql插入失败
     * @param e
     * @return
     */
    @ExceptionHandler(SqlException.class)
    public Result handleSQLIntegrityConstraintViolationException(SqlException ex) {
        ex.printStackTrace();
        return Result.error(ex.getMessage());
        // 返回自定义的错误信息
//        return Result.build("插入失败，违反唯一约束或主键冲突",ResultCodeEnum.BAD_REQUEST);
    }


    // 全局异常拦截（拦截项目中的NotLoginException异常）
    @ExceptionHandler(NotLoginException.class)
    public Result handlerNotLoginException(NotLoginException nle)
            throws Exception {
        // 打印堆栈，以供调试
        nle.printStackTrace();
        // 判断场景值，定制化异常信息
        String message = "";
        if (nle.getType().equals(NotLoginException.NOT_TOKEN)) {
            message = "未提供token";
        } else if (nle.getType().equals(NotLoginException.INVALID_TOKEN)) {
            message = "token无效";
        } else if (nle.getType().equals(NotLoginException.TOKEN_TIMEOUT)) {
            message = "token已过期";
        } else if (nle.getType().equals(NotLoginException.BE_REPLACED)) {
            message = "token已被顶下线";
        } else if (nle.getType().equals(NotLoginException.KICK_OUT)) {
            message = "token已被踢下线";
        } else {
            message = "当前会话未登录";
        }
        // 返回给前端
        return Result.build(null,500,"登录已过期，请重新登录");
    }



}
